package com.jrx.anytxn.param.service.limit.impl;

import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.common.uitl.SeqUtils;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.entity.TlBatchAdjustLimit;
import com.jrx.anytxn.param.mapper.ext.ExtTlBatchAdjustLimitMapper;
import com.jrx.anytxn.param.service.limit.IAdjustListService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

@Service
public class AdjustListServiceImpl implements IAdjustListService {

    private static final Logger logger= LoggerFactory.getLogger(AdjustListServiceImpl.class);

    @Resource
    private ExtTlBatchAdjustLimitMapper extTlBatchAdjustLimitMapper;
    @Autowired
    private ISegmentService segmentService;


    @Override
    public void transferAdjustFile(MultipartFile upfile, String orgId, String tenantId, String userName,String path,int number) throws TxnBizException {

        String oldFileName = upfile.getOriginalFilename().substring(0,upfile.getOriginalFilename().lastIndexOf("."));

        String suffix = upfile.getOriginalFilename().substring(upfile.getOriginalFilename().lastIndexOf("."));

        String dateTime = DateUtils.formatDateTime3();

        String fileName = oldFileName + "_" + dateTime + suffix;

        File dest = new File(path + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        // 保存文件
        try {
            upfile.transferTo(dest);
        } catch (IOException e) {
            logger.error("生成文件失败，",e);
            throw new TxnBizException(TxnRespCode.ERROR.getMsg());
        }

        //保存批量调额记录表
        TlBatchAdjustLimit tlBatchAdjustLimit = new TlBatchAdjustLimit();
        try {
            tlBatchAdjustLimit.setId(segmentService.getId(Constant.TL_BATCH_ADJUST_LIMIT));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(ErrorMsgConstant.ERROR_ID);
        }
        tlBatchAdjustLimit.setBatchId(SeqUtils.getSNByLength(20));
        tlBatchAdjustLimit.setTenantId(tenantId);
        //前端为null 改为使用默认
        tlBatchAdjustLimit.setOrganizationId(Constant.DEFAULT_ORG_NUMBER);
        tlBatchAdjustLimit.setFilePath(fileName);
        tlBatchAdjustLimit.setHandleStatus(Constant.HANDLE_STATUS_0);
        tlBatchAdjustLimit.setUpdateBy(userName);
        tlBatchAdjustLimit.setRecordVersionNumber(Constant.VERSION_NUMBER);
        tlBatchAdjustLimit.setTotalCount(number - 1);//文件总条数 去掉头
        tlBatchAdjustLimit.setUploadStatus(Constant.ADJUSTMENT_STATUS_S);
        tlBatchAdjustLimit.setUploadDesc("上传成功");

        extTlBatchAdjustLimitMapper.insertSelective(tlBatchAdjustLimit);
    }

    @Override
    public void buildAdjustFileLog(MultipartFile upfile,String orgId, String tenantId, String userName) throws TxnBizException {
        //保存批量调额记录表
        TlBatchAdjustLimit tlBatchAdjustLimit = new TlBatchAdjustLimit();
        try {
            tlBatchAdjustLimit.setId(segmentService.getId(Constant.TL_BATCH_ADJUST_LIMIT));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(ErrorMsgConstant.ERROR_ID);
        }
        tlBatchAdjustLimit.setBatchId(SeqUtils.getSNByLength(20));
        tlBatchAdjustLimit.setTenantId(tenantId);
        //前端为null 改为使用默认
        tlBatchAdjustLimit.setOrganizationId(orgId);
        String oldFileName = upfile.getOriginalFilename().substring(0,upfile.getOriginalFilename().lastIndexOf("."));
        tlBatchAdjustLimit.setFilePath(oldFileName);
        tlBatchAdjustLimit.setHandleStatus(Constant.HANDLE_STATUS_1);
        tlBatchAdjustLimit.setUpdateBy(userName);
        tlBatchAdjustLimit.setRecordVersionNumber(Constant.VERSION_NUMBER);
        tlBatchAdjustLimit.setTotalCount(0);
        tlBatchAdjustLimit.setUploadStatus(Constant.ADJUSTMENT_STATUS_F);
        tlBatchAdjustLimit.setUploadDesc("文件读取失败");

        extTlBatchAdjustLimitMapper.insertSelective(tlBatchAdjustLimit);
    }
}
